import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { _HttpClient, ModalHelper } from '@delon/theme';
import {SimpleTableButton, SimpleTableColumn, SimpleTableComponent} from '@delon/abc';
import { NzMessageService, NzModalService, NzTabChangeEvent, UploadFile, NzNotificationService } from 'ng-zorro-antd';
import { CommService } from 'app/service/comm/comm.service';
import { Constant, ServiceResult } from '@core/constant';
import { EmitterService } from 'app/service/emit.service';
import {ActivatedRoute, Router} from '@angular/router';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import { Timeout } from '@shared/descriptor/timeout';
import {srmbomListService} from "../../../service/srmbomList/srmbomList.service";
@Component({
  selector: 'srmbomListid-list',
  templateUrl: './srmbomListid-list.component.html',
  styleUrls: ['./srmbomListid-list.component.less']
})
export class srmbomListidComponent implements OnInit, OnDestroy {

  // region 公共字段

  visible = false;
  // pageSize
  ps = 10;
  // 搜索表单
  q: any = {
    id:"",
    lifnr:"",
    name1: '',
    matnr: '',
    zmatnr: '',
    lmatnr: '',
    maktx:'',
    lmaktx:'',
    kuanhao:''
  };

  loading = false;
  // 表格组件
  @ViewChild('st') st: SimpleTableComponent;
  // 表格数据url
  url = this.srmbomListService.listbyid();
  // region end

  // 搜索区域显示/隐藏标记
  // 显示图片
  previewVisible = false;
  // 大图地址
  previewImage = '';
  imgSuffix = '';

  years = [];
  userGroups = [];
  brands = [];
  seasons = [];
  checkedList = [];
  data = [];
  uploadExcelUrl = this.srmbomListService.uploadcarinfo();
  private sub: any;


  columns: SimpleTableColumn[] = [
    {
      title: '操作',
      buttons: [
        {
          text: '排除计算',
          type: 'link',
          click: (record: any) => this.paichu(record.id,3),
          iif: (item: any, btn: SimpleTableButton, column: SimpleTableColumn) => {
            return item.bpmStatus === '正常'
          }
        },
        {
          text: '启用计算',
          type: 'link',
          click: (record: any) => this.paichu(record.id,4),
          iif: (item: any, btn: SimpleTableButton, column: SimpleTableColumn) => {
            return item.bpmStatus === '排除'
          }
        },
        {
          text: '删除',
          type: 'link',
          click: (record: any) => this.shanchuconf(record.id),
          iif: (item: any, btn: SimpleTableButton, column: SimpleTableColumn) => {
            return item.bpmStatus === '正常'
          }
        }
      ]
    } ,
    { title: '状态', index: 'bpmStatus' },
    { title: '供应商编码', index: 'lifnr' },
    { title: '供应商名称', index: 'name1'},
    { title: '物料编码', index: 'matnr' },
    { title: '物料描述', index: 'maktx' },
    { title: '供应商料号', index: 'lmatnr' },
    { title: '供应商物料描述', index: 'lmaktx' } ,
    { title: '主物料单位', index: 'meins' },
    { title: '物料类型', index: 'mtart' },
    { title: '主物料数量', index: 'mastermenge' },
    { title: '子物料编码', index: 'zmatnr' },
    { title: '子物料描述', index: 'zmaktx' },
    { title: '子供应商物料编码', index: 'zlmatnr' },
    { title: '子供应商物料描述', index: 'zlmaktx' } ,
    { title: '子物料单位', index: 'zmeins' },
    { title: '子物料类型', index: 'zmtart' },
    { title: '子物料数量', index: 'zmastermenge' },
    { title: '款号', index: 'kuanhao' },
    { title: '交货周期', index: 'jiaohuozhouqi' },
    { title: '安全库存', index: 'anquankucun' },
    { title: '价格', index: 'jiage' },
    { title: 'BOM层级', index: 'bomcengji' },
    { title: '替代料', index: 'tidialiao' },
    { title: '通用标记', index: 'tongyong' },

  ];

  constructor(
    private trans: TranslateService,
    public http: _HttpClient,
    public msg: NzMessageService,
    private modalSrv: ModalHelper,
    private comm: CommService,
    private cnt: Constant,
    private emit: EmitterService,
    private router: Router,
    private modalService: NzModalService,
    private srmbomListService: srmbomListService,
    private route: ActivatedRoute,

    private confirmServ: NzModalService,
    private notice: NzNotificationService,
  ) {}
  syncing = false;
  lastDate = null;

  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  inter
  ngOnInit() {
    // this.imgSuffix = this.comm.getGoodsUrl();
    // this.emit.getMessage().subscribe(data => {
    //   if (data.text === 'RELOAD_GOODS_LIST') {
    //     this.st.load(1, this.q);
    //   }
    // })
    // 获取参数
    this.sub = this.route.params.subscribe(params => {
      this.q.id = params["id"];
       // 获取商品详情
      this.url=this.srmbomListService.listbyid();
    });







  }
  fullChange(val: boolean) {
    // this.scroll = val ? { y: '350px' } : { y: '230px' };
  }
  checkboxChange(list: any[]) {
    this.checkedList = list;
  }

  loaddata(wqtype){
    this.st.load(1,this.q)
  }

  downurl='';
  packatta(){
    this.loading = true;

    let conmapList = [];

    try{
      let dto1 ={
        conKey:"lmatnr",
        conValue:this.q.lmatnr,
      }
      conmapList.push(dto1);

      let dto2 ={
        conKey:"matnr",
        conValue:this.q.matnr,
      }
      conmapList.push(dto2);
      let dto21 ={
        conKey:"zmatnr",
        conValue:this.q.zmatnr,
      }
      conmapList.push(dto21);
      let dto3 ={
        conKey:"lifnr",
        conValue:this.q.lifnr,
      }

      conmapList.push(dto3);

      let dto4 ={
        conKey:"name1",
        conValue:this.q.name1,
      }

      conmapList.push(dto4);

      let dto5 ={
        conKey:"maktx",
        conValue:this.q.maktx,
      }

      conmapList.push(dto5);


      let dto6 ={
        conKey:"lmaktx",
        conValue:this.q.lmaktx,
      }

      conmapList.push(dto6);
      let dto7 ={
        conKey:"kuanhao",
        conValue:this.q.kuanhao,
      }

      conmapList.push(dto7);
    }catch (e) {

    }
    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genlfsrmbommatnr(this.q.lifnr,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }
  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }
  paichu(id,type){
    if(type==3){
      this.comm.nocount(id).pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        this.msg.success('禁用计算操作成功');
        if(data.ok){
          this.loading = false;
          this.st.load();
        }
      })
    }else{
      this.comm.count(id).pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        this.msg.success('启用计算操作成功');
        if(data.ok){
          this.loading = false;
          this.st.load();
        }
      })
    }

  }

  shanchuconf(id){
    this.confirmServ.confirm({
      nzTitle: '确定删除?',
      nzOnOk: () => {
        this.shanchu(id);
      },
    });

  }

  shanchu(id){

      this.comm.shanchu(id).pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        this.msg.success('删除成功');
        if(data.ok){
          this.loading = false;
          this.st.load();
        }
      })

    }


  close(): void {
    this.visible = false;
  }
  cancel() {
    this.repdate = null;
  }
  repdate = null;
  uploadProd(info: { file: UploadFile }) {
    if (info.file.status === 'uploading') {
      this.loading = true;
    }
    if (info.file.status === 'done') {
      this.loading = false;
     console.log("info1*********"+JSON.stringify(info.file.response[0]));
      this.st.load();
      if(info.file.response[0].piccontent==='success'){
        this.notice.create('success', '成功', info.file.response[0].piccontent);
      }else{
        this.notice.create('error', '失败', info.file.response[0].piccontent);
      }
    }
    if (info.file.status === 'error') {
      this.loading = false;
      this.notice.create('error', '失败', '导入失败');
    }
  }
}
